using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ControlGravity : ConTrolLogic
{
	public ControlGravity(int _col, int _row, Sugar[,] _arrSugar, MapLoad _map)
	{
		col = _col;
		row = _row;
		arrSugar = _arrSugar;
		mapLoad = _map;
	}
	public override void CheckLogic()
	{
		int i = col;
		for (int j = mapLoad.row  -1; j >= 0; j--)
		{
			if (arrSugar[i, j].stt == SugarStatus.HaveSomething)
			{
				int i2;
				int j2;
				if (FindNewPlace(i, j,out i2, out j2))
				{
					DUtil.TwoSugarMoveData(arrSugar[i,j], arrSugar[i2,j2]);
				}
			}
		}
	}

	private bool FindNewPlace(int i, int row, out int i2, out int j2)
	{
		Sugar sugarTran = null;
		for (int j = row + 1; j < mapLoad.row; j++)
		{
			if (j >= mapLoad.row)
			{
				break;
			}
			if (arrSugar[i, j].stt == SugarStatus.Transparent)
			{
				sugarTran = arrSugar[i, j];
			}
			else
			{
				break;
			}
		}
		if (sugarTran!=null)
		{
			i2 = sugarTran.col;
			j2 = sugarTran.row;
			return true;
		}
		else
		{
			i2 = 0;
			j2 = 0;
			return false;
		}
	}
}
